package com.wg.process.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2).globalOperationParameters(createGlobalParams()).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("flowable流程引擎")
                .description("集成flowale 流程引擎框架接口信息")
                .version("1.0")
                .build();
    }
    private List<Parameter> createGlobalParams() {
        List<Parameter> aParameters = new ArrayList<Parameter>();

        // 可以添加多个header或参数
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder.parameterType("header") // 参数类型支持header, cookie, body, query etc
                .name("crctoken") // 参数名
                .defaultValue("crctoken#000") // 默认值
                .description("header中crctoken字段").modelRef(new ModelRef("string"))// 指定参数值的类型
                .required(false).build(); // 非必需，这里是全局配置，然而在登陆的时候是不用验证的

        aParameters.add(aParameterBuilder.build());

        return aParameters;
    }
}
